package cz.su.pod.ksocializer.server.service.impl;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import cz.su.pod.ksocializer.client.service.SocialPoster;
import cz.su.pod.ksocializer.server.poster.FacebookPoster;
import cz.su.pod.ksocializer.server.poster.Poster;
import cz.su.pod.ksocializer.shared.domain.SocialNetwork;
import cz.su.pod.ksocializer.shared.domain.State;

public class SocialPosterImpl extends RemoteServiceServlet implements SocialPoster {
	private Poster facebookPoster = new FacebookPoster();
	
	@Override
	public Map<SocialNetwork, State> postMessage(String message) {
		Map<SocialNetwork, State> results = new HashMap<SocialNetwork, State>();
		
		doPostMessage(facebookPoster, message, results);
		
		return results;
	}
	
	private void doPostMessage(Poster poster, String message, Map<SocialNetwork, State> results) {
		try {
			poster.postMessage(message);
			results.put(poster.getType(), State.OK);
		} catch (Exception e) {
			e.printStackTrace();
			results.put(poster.getType(), State.ERROR);
		}
	}

}
